package org.webball.draw;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.webball.base.abstracts.AbstractBall;
import org.webball.base.interfaces.IDraw;
import org.webball.pitch.Pitch;
import org.webball.pitch.PitchConstants;
import org.webball.player.Player;

/**
 * <p>
 * Class represents draw method
 * </p>
 * 
 * @author Jakub Garczynski
 *
 */

public class Draw implements IDraw {

	static Image pitchImg = null;
	Image ballImg = null;
	Image scoreImg = null;
	Image player1Img = null;
	Image player2Img = null;
	int myteam = 0;
	
	public Draw(int team){
    		this.myteam = team;
    	}

	
	@Override
	public void draw(Graphics g, Pitch p) throws SlickException {
		
		
		//pitch
		pitchImg = new Image("pitch.png");
		scoreImg = new Image("score.png");
		pitchImg.draw();
		
    	
    	scoreImg.draw(200, 8);
    	g.setColor(Color.green);
    	g.drawString(p.getTeam(0).getPoints() + " : " + p.getTeam(1).getPoints(), 380, 9 );
    	if(p.isTacticalPhase())
    		g.setColor(Color.green);
    		g.drawString(String.valueOf(p.getTimetoEndPhase()), 580, 9);
    	g.setColor(Color.black);
    	g.drawLine(PitchConstants.firstEnd.x, PitchConstants.start.y, PitchConstants.firstEnd.x, PitchConstants.firstEnd.y);
    	
    	
    	for(int i = 0 ; i < 6 ; i++) {
    	
    	//home players
			player1Img = new Image("player1.png");
    		try {
    			Player player1 = (Player) p.getTeam(0).getPlayer(i);
				
    			//selected player1
				if(player1.isSelected()) {
					if(player1==(Player) p.getTeam(myteam).getPlayer(i)) {
						player1Img = new Image("player1_active.png");
					}
				}
    			player1Img.draw(player1.getPosition().x -player1.getSize()/2, player1.getPosition().y -player1.getSize()/2);
    			if(player1==(Player) p.getTeam(myteam).getPlayer(i)) {
    				g.drawLine(player1.getPosition().x, player1.getPosition().y, player1.getHitTarget().x, player1.getHitTarget().y);
    			}
    		}
    		catch(NullPointerException ex) {
    			
    		}
    		
    		
    	  //away players
    		player2Img = new Image("player2.png");
    		try {
    			Player player2 = (Player) p.getTeam(1).getPlayer(i);
    			
    			//selected player1
				if(player2.isSelected()) {
					if(player2==(Player) p.getTeam(myteam).getPlayer(i)) {
						player2Img = new Image("player2_active.png");
					}
				}
    			player2Img.draw(player2.getPosition().x -player2.getSize()/2, player2.getPosition().y -player2.getSize()/2);
    			if(player2==(Player) p.getTeam(myteam).getPlayer(i)) {
    				g.drawLine(player2.getPosition().x, player2.getPosition().y, player2.getHitTarget().x, player2.getHitTarget().y);
    			}
    		} 
    		catch(NullPointerException ex) {
    			
    		}
    			
    			
    		
   }    	
    	
    	//ball
    	ballImg = new Image("ball.png");
    	try {
    		AbstractBall ball = p.getBall();
    		ballImg.draw(ball.getPosition().x -ball.getSize()/2, ball.getPosition().y -ball.getSize()/2);
	    	
    		//ball target
    		g.setColor(Color.black);
	    	g.fillOval(ball.getMovementTarget().x -ball.getSize()/2, ball.getMovementTarget().y -ball.getSize()/2, ball.getSize(), ball.getSize());

    	}catch(NullPointerException ex) 
    	{
    		
    	}
    }
		
	
	}

